package com.oneday.nuo.base.security.login;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.oneday.nuo.base.common.CommonStatic;
import com.oneday.nuo.base.common.SpringUtil;
import com.oneday.nuo.base.security.interceptor.NuoFilterSecurityInterceptor;
import com.oneday.nuo.base.security.user.NuoUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**数据处理过滤器
 * 处理从前端获取的数据封装成对象
 * @author liushuai
 * @date 2020/8/20 22:40
 */
@Slf4j
@Component
public class NuoAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter {


    public NuoAuthenticationProcessingFilter() {
        super(new AntPathRequestMatcher(CommonStatic.LOGIN_URL, "POST"));
    }

    @Autowired
    @Override
    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
        super.setAuthenticationManager(authenticationManager);

    }
    @Autowired
    @Override
    public void setAuthenticationFailureHandler(AuthenticationFailureHandler authenticationFailureHandler){
        super.setAuthenticationFailureHandler(authenticationFailureHandler);
    }

    @Autowired
    @Override
    public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler authenticationSuccessHandler){
        super.setAuthenticationSuccessHandler(authenticationSuccessHandler);
    }



    @Override
    public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException {

        BufferedReader reader =   IoUtil.getReader(httpServletRequest.getInputStream(),"utf-8");

        StringBuilder data = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null){
            data.append(line);
        }

        if(StrUtil.isNotEmpty(data.toString())){
            //登录用户信息
          JSONObject json =  JSONUtil.parseObj(data.toString());
          NuoUser nuoUser = Convert.convert(NuoUser.class, json);
          return this.getAuthenticationManager().authenticate(nuoUser);
        }

        return null;
    }



}
